package com.huanf.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.huanf.domain.ExcelListWriteVo;
import com.huanf.utils.ExcelWriteUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.util.List;

/**
 * @author: 35238
 * 功能: 写Excel-easyPoi-写入(导入)集合数据类型(无数据库)
 * 时间: 2023-12-25 17:40
 */
@RestController
@Slf4j
public class ExcelListWriteController extends ExcelWriteUtil {

    //文件保存到哪里，还要指定文件名。文件后缀可以是xlsx，也可以是xls
    String PATH = "E:\\桌面\\Java-Ready\\conf\\Excel\\easyPoiListWrite.xlsx";

    @GetMapping("/list") //浏览器访问 http://localhost:8081/list
    public void simple() throws Exception {
        List<ExcelListWriteVo> userList = xxgetUsers2();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户偏好列表", "我是Sheet名"), ExcelListWriteVo.class, userList);
        //把实体类的数据写入(导出)到表格
        FileOutputStream fileOutputStream = new FileOutputStream((PATH));
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();
        workbook.close();
        log.info("导出为Excel成功，文件在: {}",PATH);
    }

}
